package com.mdd.maduoduo.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;

public class GlideUtils {

    private static GlideUtils glideUtils;

    public static GlideUtils getInstances() {
        if (glideUtils == null) {
            glideUtils = new GlideUtils();
        }
        return glideUtils;
    }

    /**
     * 加载正常图片
     *
     * @param context
     * @param imageView
     * @param imgUrl
     */
    public void loadNormalImg(Context context, ImageView imageView, String imgUrl) {
        if (isDestroy(context)) {
            return;
        }
        Glide.with(context).asBitmap().load(imgUrl).apply(new RequestOptions()
                        .priority(Priority.LOW)) // 优先级
                .into(imageView);


    }

    /**
     * 加载圆形图片
     *
     * @param context
     * @param imageView
     * @param imgUrl
     */
    public void loadRoundImg(Context context, ImageView imageView, String imgUrl) {
        if (isDestroy(context)) {
            return;
        }
//        Glide.with(context)
//                .load(content).apply(getRequestOptions())
//                .into(imageView);
        Glide.with(context)
                .asBitmap()
                .load(imgUrl)
                .apply(new RequestOptions()
                        // .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .transforms(new CenterCrop(), new CircleTransform()))
                .into(imageView);
    }

    public static boolean isDestroy(Context mContext) {
        if (mContext == null) {
            return true;
        }
        if (mContext instanceof Activity) {
            Activity mActivity = (Activity) mContext;
            if (mActivity == null ||
                    mActivity.isFinishing() ||
                    (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mActivity.isDestroyed())) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }
}
